/*
Ajax 三级省市联动
http://code.ciaoca.cn/
日期：2012-7-18

settings 参数说明
-----
url:省市数据josn文件路径
prov:默认省份
city:默认城市
dist:默认地区（县）
nodata:无数据状态
required:必选项
------------------------------ */
(function ($) {
    $.fn.citySelect = function (settings) {
        if (this.length < 1) { return; };

        // 默认值  url: "../Css/assets/js/CityJson.txt",   url: "../Css/assets/js/city.min.js",
        settings = $.extend({
            url: "/Css/assets/js/CityJson.txt",
            prov: null,
            city: null,
            dist: null,
            nodata: null,
            required: true
        }, settings);
        var box_obj = this;
        var prov_obj = box_obj.find(".prov");
        var city_obj = box_obj.find(".city");
        var dist_obj = box_obj.find(".dist");
        var final_city_id = box_obj.find(".cityid");
        var prov_val = settings.prov;
        var city_val = settings.city;
        var dist_val = settings.dist;
        var select_prehtml = "<option value='000000'>全国</option>";//默认全国
        var city_json;

        // 赋值市级函数
        var cityStart = function () {
            var prov_id = prov_obj.get(0).selectedIndex - 1;
            if (!settings.required) {
                prov_id--;
            };
            city_obj.empty().attr("disabled", true);
            dist_obj.empty().attr("disabled", true);

            if (prov_id < 0 || typeof (city_json[prov_id].city) == "undefined") {
                if (settings.nodata == "none") {
                    city_obj.css("display", "none");
                    dist_obj.css("display", "none");
                } else if (settings.nodata == "hidden") {
                    city_obj.css("visibility", "hidden");
                    dist_obj.css("visibility", "hidden");
                };
                return;
            };

            // 遍历赋值市级下拉列表
            temp_html = "<option value='000000'>请选择</option>";
            $.each(city_json[prov_id].city, function (i, city) {
                temp_html += "<option value='" + city.cityId + "'>" + city.cityName + "</option>";
            });
            city_obj.html(temp_html).attr("disabled", false).css({ "display": "", "visibility": "" });
            distStart();
        };

        ///设置页面城市信息
        var SetCityID = function ()
        {
            var cityId = "000000";
            var cityName = "全国";
            //省为可选状态
            if (prov_obj.attr("disabled") != "disabled" && prov_obj.val() != "000000")
            {
                cityId = prov_obj.val();
                cityName = prov_obj.find("option:selected").text();
            }
            //市为可选状态
            if (city_obj.attr("disabled") != "disabled" && city_obj.val() != "000000")
            {
                cityId = city_obj.val();               
                cityName = cityName + "," + city_obj.find("option:selected").text();
            }
            //区为可选状态
            if (dist_obj.attr("disabled") != "disabled" && dist_obj.val() != "000000") {
                cityId = dist_obj.val();
                cityName = cityName + "," + dist_obj.find("option:selected").text();
            }

            box_obj.find('.hid_cityid').val(cityId);
            box_obj.find('.hid_cityname').val(cityName);
        }

        // 赋值地区（县）函数
        var distStart = function () {
            var prov_id = prov_obj.get(0).selectedIndex - 1;
            var city_id = city_obj.get(0).selectedIndex - 1;
            if (!settings.required) {
                prov_id--;
                city_id--;
            };
            dist_obj.empty().attr("disabled", true);

            if (prov_id < 0 || city_id < 0 || typeof (city_json[prov_id].city[city_id].dist) == "undefined") {
                if (settings.nodata == "none") {
                    dist_obj.css("display", "none");
                } else if (settings.nodata == "hidden") {
                    dist_obj.css("visibility", "hidden");
                };
                return;
            };
            // 遍历赋值市级下拉列表
            temp_html = "<option value='000000'>请选择</option>";
            $.each(city_json[prov_id].city[city_id].dist, function (i, dist) {
                temp_html += "<option value='" + dist.cityId + "'>" + dist.cityName + "</option>";
            });
            dist_obj.html(temp_html).attr("disabled", false).css({ "display": "", "visibility": "" });
            
        };

        var init = function () {
            // 遍历赋值省份下拉列表
            temp_html = select_prehtml;
            $.each(city_json, function (i, prov) {
                temp_html += "<option value='" + prov.cityId + "'>" + prov.cityName + "</option>";
            });
            prov_obj.html(temp_html);

            // 若有传入省份与市级的值，则选中。（setTimeout为兼容IE6而设置）
            setTimeout(function () {
                if (settings.prov != null) {
                    prov_obj.val(settings.prov);
                    cityStart();
                    setTimeout(function () {
                        if (settings.city != null) {
                            city_obj.val(settings.city);
                            distStart();
                            setTimeout(function () {
                                if (settings.dist != null) {
                                    dist_obj.val(settings.dist);
                                };
                            }, 1);
                        };
                    }, 1);
                };
            }, 1);

            // 选择省份时发生事件
            prov_obj.bind("change", function () {
                cityStart();
                SetCityID();
            });

            // 选择市级时发生事件
            city_obj.bind("change", function () {
                distStart();
                SetCityID();
            });

            // 选择区级时发生事件
            dist_obj.bind("change", function () {
                SetCityID();
            });
        };

        // 设置省市json数据
        if (typeof (settings.url) == "string") {
            $.getJSON(settings.url, function (json) {
                city_json = json;
                init();
            });
        } else {
            city_json = settings.url;
            init();
        };
    };
})(jQuery);